package com.ruoyi.SuveryInterface;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.AliYunOss.AliOssUtil;
import com.ruoyi.SuveryInterface.CommercialSuvery.CommercialNameAndCard;
import com.ruoyi.SuveryInterface.CreditSuvery.CreditNameAndCard;
import com.ruoyi.SuveryInterface.DegreesSuvery.DegreesSuveryThree;
import com.ruoyi.SuveryInterface.EducationSuvery.EducationSuveryThree;
import com.ruoyi.SuveryInterface.IdentitySuvery.IdentitySuveryImp;
import com.ruoyi.SuveryInterface.Imp.domain.*;
import com.ruoyi.SuveryInterface.Imp.mapper.*;
import com.ruoyi.SuveryInterface.NegativeSuvery.NegativeSuvery;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.doword.controller.WordExportController;
import com.ruoyi.order.Utils.OrderUtils;
import com.ruoyi.order.domain.SysOrderOne;
import com.ruoyi.order.mapper.SysOrderOneMapper;
import com.ruoyi.packages.Utils.SurveyUtils;
import com.ruoyi.packages.domain.SysSurvey;
import com.ruoyi.packages.domain.SysSurveyPackage;
import com.ruoyi.packages.mapper.SysPackageMapper;
import com.ruoyi.packages.mapper.SysSurveyMapper;
import com.ruoyi.report.domain.SysReport;
import com.ruoyi.report.domain.SysReportDetails;
import com.ruoyi.report.mapper.SysReportDetailsMapper;
import com.ruoyi.report.mapper.SysReportMapper;
import com.ruoyi.store.mapper.SysStoreMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.packages.Utils.SurveyUtils.*;

/**
 * @Description:
 * @Author: 吕宇宏
 * @Date: 2024/7/1 17:41
 */
@RestController
@RequestMapping("/survey/")
public class SuveryDemo {

    @Autowired
    private SysCreditMapper creditMapper;


    @Autowired
    private SysDegreesMapper degreesMapper;

    @Autowired
    private SysDegreeDetailsMapper degreeDetailsMapper;

    @Autowired
    private SysEducationDetailsMapper educationDetailsMapper;

    @Autowired
    private SysCommercialMapper commercialMapper;

    @Autowired
    private SysLegalInfosMapper legalInfosMapper;
    @Autowired
    private SysShareholderInfosMapper shareholderInfosMapper;

    @Autowired
    private SysManagerInfosMapper managerInfosMapper;

    @Autowired
    private SysPunishedsMapper punishedsMapper;

    @Autowired
    private SysPunishBreaksMapper punishBreaksMapper;

    @Autowired
    private SysAdminPenaltyMapper adminPenaltyMapper;

    @Autowired
    private SysIdentityMapper identityMapper;

    @Autowired
    private SysIdentityCasesMapper identityCasesMapper;

    @Autowired
    private SysIdentitySxbzxrMapper identitySxbzxrMapper;

    @Autowired
    private SysIdentityXgbzxrMapper xgbzxrMapper;

    @Autowired
    private SysIdentityDsrxxMapper dsrxxMapper;


    @Autowired
    private SysNegativeMapper negativeMapper;

    @Autowired
    private SysNegativeDetailsMapper negativeDetailsMapper;


    @Autowired
    private SysReportDetailsMapper reportDetailsMapper;

    @Autowired
    private SysOrderOneMapper orderOneMapper;

    @Autowired
    private SysPackageMapper packageMapper;

    @Autowired
    private SysSurveyMapper surveyMapper;

    @Autowired
    private SysOrderOneMapper sysOrderOneMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SysStoreMapper sysStoreMapper;

    @Autowired
    private WordExportController wordExportController;


    @Autowired
    private OrderUtils orderUtils;


    @Autowired
    private SurveyUtils surveyUtils;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private SysReportMapper reportMapper;

    @Autowired
    private AliOssUtil aliOssUtil;

    @Anonymous
    @GetMapping("/demo01")
    public  AjaxResult demo01(HttpServletRequest request, HttpServletResponse httpServletResponse){

        List<Long> ids = new ArrayList<>();
        ids.add(128l);
        ids.add(127l);
        List<SysOrderOne> list = orderOneMapper.selectSysOrderOneByIds(ids);


        List<Long> OrderIds = list.stream().map(SysOrderOne::getId).collect(Collectors.toList());

        //根据orderIds查询对应的报告信息，如果有，有几个
        List<SysOrderOne> orderOnes = orderOneMapper.selectSysOrderOneByIds(OrderIds);
        Map<Long, SysOrderOne> orderMap = orderOnes.stream().collect(Collectors.toMap(SysOrderOne::getId, c -> c));

        List<SysReport> reports =reportMapper.selectSysReportByOrderIds(OrderIds);

        Map<Long,List<String>> reportMap = new HashMap<>();
        for (SysReport report : reports) {
            if(!reportMap.containsKey(report.getOrderId())) {
                List<String> urlList = new ArrayList<>();
                reportMap.put(report.getOrderId(),urlList);

            }
            if (report.getUrl()!=null) {
                reportMap.get(report.getOrderId()).add(report.getUrl());
            }

        }

        aliOssUtil.createZipAndSendMore(httpServletResponse,reportMap,orderMap);
        return success("下载中");

//        Long orderId = 128l;
//        //首先判断当前订单是否存在，订单状态是否为已经出报告
//        SysOrderOne sysOrderOne = orderOneMapper.selectSysOrderOneById(orderId);
//        if (sysOrderOne==null) {
//            throw new ServiceException("订单不存在");
//        }
//
//        Long orderStatus = sysOrderOne.getOrderStatus();
//        if (!orderStatus.equals(3l)) {
//            //说明订单还未出报告
//            throw new ServiceException("订单报告未生成，请稍等");
//        }
//
//
//        //查询数据库对应的url
//        List<SysReport> list =reportMapper.selectListByOrderId(orderId);
//
//        if (CollectionUtils.isEmpty(list)) {
//            throw new ServiceException("当前订单并无报告");
//        }
//        List<String> collect = list.stream().map(SysReport::getUrl).collect(Collectors.toList());
//
//        if (collect.size()>0) {
//            aliOssUtil.createZipAndSend(httpServletResponse, collect, sysOrderOne);
//            return success("下载中");
//        }
//
//
//
//
//
//        return error("下载失败");


//        SysOrderOne orderOne = orderOneMapper.selectSysOrderOneById(127l);
//        String s = JSONObject.toJSONString(orderOne);
//        rabbitTemplate.convertAndSend("backtone.queue",s);
////        surveyUtils.SurveyImp(orderOne,request,httpServletResponse);

////
////        String orderReportID = orderUtils.getOrderReportID(128l);
////        System.out.println(orderReportID);
////
//        SysOrderOne orderOne = orderOneMapper.selectSysOrderOneById(127l);
//
//        AjaxResult word=null;
//        try {
//             word = wordExportController.getWord(request, httpServletResponse, orderOne);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//
//        Map<String,Object> data = (Map<String, Object>) word.get("data");
//
//        Object wordUrl = data.get("wordUrl");
//        Object pdfUrl = data.get("pdfUrl");
//        Object htmlUrl = data.get("htmlUrl");
//
//
//        System.out.println(word);


//
//        int i = sysStoreMapper.PayOrder(1l, new BigDecimal(100.00));
//
//        if (i<=0) {
//            throw new ServiceException("余额不足，更新失败");
//        }
//        else {
//            return "成功";
//        }


//      DegreesSuveryThree degreesSuveryThree = new DegreesSuveryThree(degreesMapper,degreeDetailsMapper);
//        CommercialNameAndCard commercialNameAndCard = new CommercialNameAndCard(commercialMapper,legalInfosMapper,shareholderInfosMapper,managerInfosMapper,punishedsMapper,punishBreaksMapper,adminPenaltyMapper);
//     CreditNameAndCard creditNameAndCard = new CreditNameAndCard(creditMapper);
//      EducationSuveryThree educationSuveryThree = new EducationSuveryThree(educationDetailsMapper);
//        IdentitySuveryImp identitySuveryImp = new IdentitySuveryImp(identityMapper,identityCasesMapper,dsrxxMapper,identitySxbzxrMapper,xgbzxrMapper);
//        NegativeSuvery negativeSuvery = new NegativeSuvery(negativeMapper,negativeDetailsMapper);

//        1.学位查询：
//        SysDegrees degreesResult = null;
//        try {
//            degreesResult = degreesSuveryThree.QueryDegrees("王大通", "101081201505002521", "370783199111223578", 1l);
//        }
//        catch (ServiceException e) {
//            //参数问题 ：不重试
//            e.printStackTrace();
//        }
//        catch (RuntimeException e) {
//            //返回值问题，重试
//            e.printStackTrace();
//        }
//        catch (Exception e) {
//            //返回值问题，重试
//            e.printStackTrace();
//        }
//        System.out.println("学位查询结果："+degreesResult);
//
//        String code = "{\"code\":\"0000\",\"message\":\"{{\\\"result\\\":\\\"0\\\",\\\"resultMsg\\\":\\\"唯一匹配/多个匹配\\\",\\\"data\\\":[{\\\"xm\\\":\\\"张三\\\",\\\"xb\\\":\\\"男\\\",\\\"csrq\\\":\\\"1988-05-19\\\",\\\"xwsydw\\\":\\\"北京大学\\\",\\\"xkzy\\\":\\\"软件工程\\\",\\\"xxgdlx\\\":\\\"博士\\\",\\\"xwlb\\\":\\\"工学博士学位\\\",\\\"xwzsbh\\\":\\\"8868***686\\\",\\\"hxwrq\\\":\\\"2008-08-08\\\"},{\\\"xm\\\":\\\"张三\\\",\\\"xb\\\":\\\"男\\\",\\\"csrq\\\":\\\"1988-05-19\\\",\\\"xwsydw\\\":\\\"北京大学\\\",\\\"xkzy\\\":\\\"软件工程\\\",\\\"xxgdlx\\\":\\\"博士\\\",\\\"xwlb\\\":\\\"工学博士学位\\\",\\\"xwzsbh\\\":\\\"8868***686\\\",\\\"hxwrq\\\":\\\"2008-08-08\\\"}]}}\",\"out_trade_no\":\"20161026110000000032\",\"sign\":\"PBPQIn7/loK6gc0ku/i13EX6K8vSn+lnhY6mGZZA0Bk3OSS1WZK41zDr1XKnOW/N9OHsxL7/yutyZgwwMOUjltp7Nn6CmHJw3+W3bjmzwjrXTv0G2iSRPWxGHwbDJsqkALOIzbjDW62/yRh8pSh/RsM6opX8Pa/umLJBMiYMqyQ=\",\"tran_amt\":\"0.500\",\"tran_time\":\" Wed Oct 26 11:00:01 CST 2016\"}";
//
//
//        SysDegrees degreesEntity = getDegreesEntity(code);
//
//
//
//        degreesMapper.insertSysDegrees(degreesEntity);
//        List<com.alibaba.fastjson.JSONObject> degreeDetails = degreesEntity.getJsonObjects();
////        degreeDetailsService.saveBatch(degreeDetails);
//        if (!CollectionUtils.isEmpty(degreeDetails)) {
//            for (com.alibaba.fastjson.JSONObject degreeDetail : degreeDetails) {
//                degreeDetail.put("sysDegreesId",degreesEntity.getId());
//            }
//            degreeDetailsMapper.insertBatch(degreeDetails);
//        }


        //2.个人工商查询


//        SysCommercial commercialResult = null;
//        try {
//            commercialResult = commercialNameAndCard.QueryCommercial("吕宇宏", "141124200308110076", 100l);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//        System.out.println("工商查询结果："+commercialResult);
        //3.信贷风险查询：
//        SysCreditEntity creditResult = null;
//        try {
//            creditResult = creditNameAndCard.QueryCredit("王兴通", "14124200308110076",3l);
//        } catch (ServiceException e) {
//            //说明是参数问题：直接插入异常表就行
//
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println("信贷风险结果："+creditResult);
        //4.学历查询
//        SysEducation educationResult = null;
//        try {
//            educationResult = educationSuveryThree.QueryEducation("王大通", "1010812015052511", "1411242222222", 200l);
//        }catch (ServiceException e) {
//            //参数有问题
//            e.printStackTrace();
//        }
//        catch (RuntimeException e) {
//            //返回值有问题
//            e.printStackTrace();
//        }
//        catch (Exception e) {
//            //调用发生问题
//            e.printStackTrace();
//        }
//        System.out.println("学历查询结果："+educationResult);
       // 5.身份查询：
//        List<SysIdentity> sysIdentities=null;
//        try {
//           sysIdentities = identitySuveryImp.QueryIndentity("吕宇宏", "30783199111223578", 1);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println("身份查询结果："+sysIdentities);
////
       // 6.公共不安查询
//        SysNegative sysNegative = null;
//        try {
//            sysNegative = negativeSuvery.QueryNegative("王兴通", "370783199111223578", 1, 1l);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println("公共不安查询结果："+sysNegative);
//        SurveyUtils surveyUtils =
//                new SurveyUtils(surveyMapper,packageMapper,creditMapper,
//                        degreesMapper, degreeDetailsMapper,educationDetailsMapper,
//                        commercialMapper,legalInfosMapper, shareholderInfosMapper,
//                        managerInfosMapper, punishedsMapper, punishBreaksMapper,
//                        adminPenaltyMapper, identityMapper,  identityCasesMapper,
//                        identitySxbzxrMapper,xgbzxrMapper, dsrxxMapper,negativeMapper,
//                        negativeDetailsMapper,reportDetailsMapper,sysOrderOneMapper,redisTemplate);
//
//      Map<Long,List<Long>> map  = new HashMap<>();
//      List<Long> surveyIds = new ArrayList<>();
//      surveyIds.add(1l);
//        surveyIds.add(2l);
//        surveyIds.add(3l);
//        surveyIds.add(4l);
//        surveyIds.add(5l);
//        map.put(1l,surveyIds);
//        surveyUtils.restartFailTask(map);

//        LocalDateTime now = LocalDateTime.now();
//
//        // 计算七天前的时间
//        LocalDateTime sevenDaysAgo = now.minus(7, ChronoUnit.DAYS);
//
//        List<SysOrderOne> orderOnes=orderOneMapper.selectSysOrderOneByNameAndCard("王兴通","370783199111223578","13643669790",sevenDaysAgo);
//        if (CollectionUtils.isNotEmpty(orderOnes)) {
//            //将历史订单的接口获取到对应的接口放入map中
//            SysOrderOne sysOrderOne = orderOnes.get(0);
//
//
//        }
//
//
//        //1.信贷风险查询mapper
//        Long orderId = 1l;
//       List<SysCredit> credits= creditMapper.selectSysCreditByOrderId(orderId);
//
//        System.out.println(credits);
//
//
//        //2.学位核实查询
//
//       List<SysDegrees> degrees= degreesMapper.selectSysDegreesByOrderId(1l);
//
//       //根据学位查询id查询明细
//        Long degreeId = degrees.get(0).getId();
//
//        List<SysDegreeDetails> degreeDetails=degreeDetailsMapper.selectDegreesDetailsByDegreesId(degreeId);
//
//        //3.学历查询：
//       List<SysEducation> educations= educationDetailsMapper.selectEducationByOrderId(orderId);
//
//       //根据学历查询id获取详细数据
//        Long educationId = educations.get(0).getId();
//       List<SysEducationDetails> educationDetails= educationDetailsMapper.selectDetailsByEducationId(educationId);
//
//
//
//       //4.个人工商查询
//
//       List<SysCommercial> commercials= commercialMapper.selectSysCommercialByOrderId(orderId);
//
//        Long commercialId = commercials.get(0).getId();
//
//        //根据id查询对应详细数据
//        //4.1 legalInfos:
//       List<SysLegalInfos> legalInfos= legalInfosMapper.selectSysLegalInfosByCommercialId(commercialId);
//
//       //4.2  shareholderInfos
//       List<SysShareholderInfos> shareholderInfos= shareholderInfosMapper.selectSysShareholderInfosByCommercialId(commercialId);
//
////       //4.3  managerInfos
//       List<SysManagerInfos> managerInfos= managerInfosMapper.selectSysManagerInfosByCommercialId(commercialId);
//
//       //4.4 punisheds
//
//       List<SysPunisheds> punisheds= punishedsMapper.selectSysPunishedsByCommercialId(commercialId);
//
//       //4.5 punishBreaks
//       List<SysPunishBreaks> punishBreaks= punishBreaksMapper.selectSysPunishBreaksByCommercialId(commercialId);
//
//       //4.6  adminPenalty
//        List<SysAdminPenalty> adminPenalties= adminPenaltyMapper.selectSysAdminPenaltyByCommercialId(commercialId);
//
//
//        //5.公安不良查询
//        List<SysNegative> negatives=negativeMapper.selectSysNegativeByOrderId(orderId);
//        Long negativeId = negatives.get(0).getId();
//
//        List<SysNegativeDetails> sysNegativeDetails=negativeDetailsMapper.selectSysNegativeDetailsByNegativeId(negativeId);
//
//
//        //6.身份验证查询
//        List<SysIdentity> identities=identityMapper.selectSysIdentityByOrderId(orderId);
//
//        Long identityId = identities.get(0).getId();
//        //根据id查询详细数据
//        //6.1  case  dzmx civil implement 三种类型 通过type判断
//        //type 0 dzmx  / 1 civil /2 implement
//        List<SysIdentityCases> identityCases = identityCasesMapper.selectSysIdentityCasesByIdentityId(identityId,"0");
//
//        Long casesId = identityCases.get(0).getId();
//        //6.2 dsrxx  根据cases的id 查
//       List<SysIdentityDsrxx> identityDsrxxes= dsrxxMapper.selectSysIdentityDsrxxByCasesId(casesId);
//
//        //6.3 sxbzxr
//
//        List<SysIdentitySxbzxr> identitySxbzxrs=identitySxbzxrMapper.selectSysIdentitySxbzxrByIdentityId(identityId);
//
//        //6.4  xgbzxr
//
//       List<SysIdentityXgbzxr> xgbzxrs= xgbzxrMapper.selectSysIdentityXgbzxrByIdentityId(identityId);


//        //风险评估查询
//       List<SysReportDetails> report01 =
//               reportDetailsMapper.selectSysReportByOrderIdAndType(42l,"0");


        //缓存复用测试：
//        //todo 根据姓名和身份证号查询七天内是否有相同的订单
//        // 获取当前时间
//        SysOrderOne order = orderOneMapper.selectSysOrderOneById(43l);
//
//        Long orderId = order.getId();
//
//        //套餐id
//        Long setmealId = order.getSetmealId();
//
//        String name = order.getCdName();
//        String phone = order.getPhone();
//        String card = order.getIdNumber();
//        String certificate = order.getCertificate();
//        String degreeCerNo = order.getDegreeCerNo();
//
//
//        //根据套餐id查询对应的接口
//        List<Long> packageIds =  new ArrayList<>();
//        packageIds.add(setmealId);
//        List<SysSurveyPackage> surveyPackages = packageMapper.selectSysPackageSuvery(packageIds);
//        Set<Long> surveyIds = surveyPackages.stream().map(SysSurveyPackage::getSurveyId).collect(Collectors.toSet());
//
//        //封装单个接口的报告情况：
//        //填充公共字段属性
//        SysReportDetails sysReportDetails = new SysReportDetails();
//        sysReportDetails.setOrderId(orderId);
//        if (order.getParentId()!=null) {
//            sysReportDetails.setParentId(order.getParentId());
//        }
//        sysReportDetails.setCreateBy(order.getCreateBy());
//        sysReportDetails.setCreateTime(new Date());
//        sysReportDetails.setDeptId(order.getDeptId());
//        sysReportDetails.setUserId(order.getUserId());
//
//        List<SysReportDetails> reportDetailsList = new ArrayList<>();
//
//        //查询所有的调查接口
//        List<SysSurvey> sysSurveys = surveyMapper.selectAllSysSurveyList();
//        Map<Long, String> surveyMap = sysSurveys.stream().collect(Collectors.toMap(SysSurvey::getId, c -> c.getName()));
//
//
//        LocalDateTime now = LocalDateTime.now();
//
//        // 计算七天前的时间
//        LocalDateTime sevenDaysAgo = now.minus(7, ChronoUnit.DAYS);
//
//        List<SysOrderOne> orderOnes=orderOneMapper.selectSysOrderOneByNameAndCard(order.getCdName(),order.getIdNumber(),order.getPhone(),sevenDaysAgo);
//        Map<Long,Long> historysurveyMap = new HashMap<>();
//        if (CollectionUtils.isNotEmpty(orderOnes)) {
//            //将历史订单的接口获取到对应的接口放入map中
//            SysOrderOne sysOrderOne = orderOnes.get(0);
//            Long orderHistoryId = sysOrderOne.getId();
//            Long historyPackageId = sysOrderOne.getSetmealId();
//
//            //根据套餐id查询接口
//            List<Long> historyPackageIds = new ArrayList<>();
//            historyPackageIds.add(historyPackageId);
//            List<SysSurveyPackage> historysurveyPackages = packageMapper.selectSysPackageSuvery(historyPackageIds);
//            Set<Long> collect = historysurveyPackages.stream().map(SysSurveyPackage::getSurveyId).collect(Collectors.toSet());
//
//            for (Long aLong : collect) {
//                historysurveyMap.put(aLong,orderHistoryId);
//            }
//
//
//        }

//        List<SysCredit> credits = new ArrayList<>();
//        if (historysurveyMap.containsKey(1l)) {
//            //则走对应的历史查询
//            Long historyOrderId = historysurveyMap.get(1l);
//            //todo 复制数据数据并插入新的
//            credits = creditMapper.selectSysCreditByOrderId(historyOrderId);
//
//            if (CollectionUtils.isNotEmpty(credits)) {
//                SysCredit historyCredit = credits.get(0);
//                SysCredit sysCredit = new SysCredit();
//                BeanUtils.copyProperties(historyCredit,sysCredit);
//                sysCredit.setOrder_id(order.getId());
//                sysCredit.setCreate_time(new Date());
//
//
//                creditMapper.insertSysCredit(sysCredit);
//
//                //todo 复制风险评估数据并插入
//
//                List<SysReportDetails> reportDetails = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "8");
//
//                if(CollectionUtils.isNotEmpty(reportDetails)) {
//                    List<SysReportDetails> newReportDetails = new ArrayList<>();
//
//                    for (SysReportDetails reportDetail : reportDetails) {
//                        SysReportDetails sysReportDetails1 = new SysReportDetails();
//                        BeanUtils.copyProperties(reportDetail,sysReportDetails1);
//
//                        sysReportDetails1.setOrderId(order.getId());
//                        sysReportDetails1.setUserId(SecurityUtils.getUserId());
//                        sysReportDetails1.setDeptId(SecurityUtils.getDeptId());
//                        sysReportDetails1.setCreateTime(new Date());
//                        sysReportDetails1.setCreateBy(SecurityUtils.getUsername());
//                        sysReportDetails1.setId(null);
//
//                        newReportDetails.add(sysReportDetails1);
//
//
//                    }
//
//                    reportDetailsMapper.insertBatchSysReportDetails(newReportDetails);
//
//
//                }
//
//
//            }



//
//        }
//        else if (CollectionUtils.isEmpty(credits)){
//            //信贷风险查询
//            SysCreditEntity sysCreditEntity=null;
//            CreditNameAndCard creditNameAndCard = new CreditNameAndCard(creditMapper);
//            SysReportDetails sysReportDetails9 = new SysReportDetails();
//            BeanUtils.copyProperties(sysReportDetails,sysReportDetails9);
//            sysReportDetails9.setSurveyId(1l);
//            sysReportDetails9.setSurveyName(surveyMap.get(1l));
//            sysReportDetails9.setSurveyStatus(1l);  //设置为调查中
//            sysReportDetails9.setType("8");  //个人信息风险评估
//            sysReportDetails9.setSurveyResult(2l);
//
//            try {
//                sysCreditEntity = creditNameAndCard.QueryCredit(name, card, orderId);
//            } catch (Exception e) {
//                throw new ServiceException("信贷风险查询失败");
//            }
//            if (sysCreditEntity==null||!sysCreditEntity.getCode().equals("0000")) {
//                throw new ServiceException("信贷查询失败");
//            }
//
//            //todo 风险评估和表插入
//            if (sysCreditEntity!=null) {
//                String dataResult = sysCreditEntity.getDataResult();
//                if (dataResult.equals("Y")){
//                    sysReportDetails9.setSurveyResult(4l);
//                }
//
//
//            }
//            reportDetailsList.add(sysReportDetails9);
//        }

//        List<SysEducation> educations = new ArrayList<>();
//        if (historysurveyMap.containsKey(2l)) {
//            //则走对应的历史查询
//            Long historyOrderId = historysurveyMap.get(2l);
//            //todo 复制数据数据并插入新的
//            educations = educationDetailsMapper.selectEducationByOrderId(historyOrderId);
//
//            if (CollectionUtils.isNotEmpty(educations)) {
//                SysEducation historyEducation = educations.get(0);
//                SysEducation sysEducation = new SysEducation();
//                BeanUtils.copyProperties(historyEducation,sysEducation);
//                sysEducation.setOrderId(order.getId());
//                sysEducation.setCreateTime(new Date());
//
//                educationDetailsMapper.insertEducation(sysEducation);
//
//                //封装对应的学历详细表：
//                List<SysEducationDetails> historyeducationDetails = educationDetailsMapper.selectDetailsByEducationId(historyEducation.getId());
//                if (CollectionUtils.isNotEmpty(historyeducationDetails)) {
//                    List<SysEducationDetails> newEducationDetailsList = new ArrayList<>();
//                    for (SysEducationDetails historyeducationDetail : historyeducationDetails) {
//                        SysEducationDetails newEducationDetails = new SysEducationDetails();
//                        BeanUtils.copyProperties(historyeducationDetail,newEducationDetails);
//                        newEducationDetails.setSysEducationId(sysEducation.getId());
//                        newEducationDetailsList.add(newEducationDetails);
//                    }
//                    educationDetailsMapper.insertBatchEducationDetails(newEducationDetailsList);
//                }
//
//                //todo 复制风险评估数据并插入
//
//                List<SysReportDetails> reportDetails = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "11");
//
//                if(CollectionUtils.isNotEmpty(reportDetails)) {
//                    List<SysReportDetails> newReportDetails = new ArrayList<>();
//
//                    for (SysReportDetails reportDetail : reportDetails) {
//                        SysReportDetails sysReportDetails1 = new SysReportDetails();
//                        BeanUtils.copyProperties(reportDetail,sysReportDetails1);
//
//                        sysReportDetails1.setOrderId(order.getId());
//                        sysReportDetails1.setUserId(SecurityUtils.getUserId());
//                        sysReportDetails1.setDeptId(SecurityUtils.getDeptId());
//                        sysReportDetails1.setCreateTime(new Date());
//                        sysReportDetails1.setCreateBy(SecurityUtils.getUsername());
//
//                        newReportDetails.add(sysReportDetails1);
//
//
//                    }
//
//                    reportDetailsMapper.insertBatchSysReportDetails(newReportDetails);
//
//
//                }
//
//
//            }
//
//
//
//
//        }
//        else if(CollectionUtils.isEmpty(educations)) {
//            //学历信息核实
//            SysEducation sysEducation =null;
//            EducationSuveryThree educationSuveryThree = new EducationSuveryThree(educationDetailsMapper);
//            try {
//                sysEducation = educationSuveryThree.QueryEducation(name, certificate, card, orderId);
//
//            } catch (Exception e) {
//                throw new ServiceException("学历核实查询失败");
//            }
//            if (sysEducation==null||(!sysEducation.getCode().equals("0000")&&!sysEducation.getCode().equals("0001")) ){
//                throw new ServiceException("学历核实查询失败");
//            }
//            //todo 表插入
//
//            SysReportDetails sysReportDetails12 = new SysReportDetails();
//            BeanUtils.copyProperties(sysReportDetails,sysReportDetails12);
//            sysReportDetails12.setSurveyId(2l);
//            sysReportDetails12.setSurveyName(surveyMap.get(2l));
//            sysReportDetails12.setSurveyStatus(1l);  //设置为调查中
//            sysReportDetails12.setType("11");  //个人信息风险评估
//            sysReportDetails12.setSurveyResult(4l);
//
//            if (sysEducation.getCode().equals("0000")) {
//                sysReportDetails12.setSurveyResult(2l);
//            }
//
//
//            reportDetailsList.add(sysReportDetails12);
//
//
//
//        }
//
//        //优先走缓存：
//        List<SysDegrees> degrees = new ArrayList<>();
//        if (historysurveyMap.containsKey(3l)) {
//            //则走对应的历史查询
//            Long historyOrderId = historysurveyMap.get(3l);
//            //todo 复制数据数据并插入新的
//            degrees = degreesMapper.selectSysDegreesByOrderId(historyOrderId);
//
//
//            if (CollectionUtils.isNotEmpty(degrees)) {
//                SysDegrees historyDegrees = degrees.get(0);
//                SysDegrees sysDegrees = new SysDegrees();
//                BeanUtils.copyProperties(historyDegrees,sysDegrees);
//                sysDegrees.setOrderId(order.getId());
//                sysDegrees.setCreateTime(new Date());
//
//                degreesMapper.insertSysDegrees(sysDegrees);
//
//
//                //封装对应的学位详细表：
//                List<SysDegreeDetails> historyDegreeDetails = degreeDetailsMapper.selectDegreesDetailsByDegreesId(historyDegrees.getId());
//                if (CollectionUtils.isNotEmpty(historyDegreeDetails)) {
//                    List<SysDegreeDetails> newDegreeDetailsList = new ArrayList<>();
//                    for (SysDegreeDetails historyDegreeDetail : historyDegreeDetails) {
//                        SysDegreeDetails newDegreeDetails = new SysDegreeDetails();
//                        BeanUtils.copyProperties(historyDegreeDetail,newDegreeDetails);
//                        newDegreeDetails.setSysDegreesId(sysDegrees.getId());
//                        newDegreeDetailsList.add(newDegreeDetails);
//                    }
//                    degreeDetailsMapper.insertBatchSysDegreeDetails(newDegreeDetailsList);
//
//
//
//                }
//
//                //todo 复制风险评估数据并插入
//
//                List<SysReportDetails> reportDetails = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "12");
//
//                if(CollectionUtils.isNotEmpty(reportDetails)) {
//                    List<SysReportDetails> newReportDetails = new ArrayList<>();
//
//                    for (SysReportDetails reportDetail : reportDetails) {
//                        SysReportDetails sysReportDetails1 = new SysReportDetails();
//                        BeanUtils.copyProperties(reportDetail,sysReportDetails1);
//
//                        sysReportDetails1.setOrderId(order.getId());
//                        sysReportDetails1.setUserId(SecurityUtils.getUserId());
//                        sysReportDetails1.setDeptId(SecurityUtils.getDeptId());
//                        sysReportDetails1.setCreateTime(new Date());
//                        sysReportDetails1.setCreateBy(SecurityUtils.getUsername());
//
//                        newReportDetails.add(sysReportDetails1);
//
//
//                    }
//
//                    reportDetailsMapper.insertBatchSysReportDetails(newReportDetails);
//
//
//                }
//
//
//            }
//
//
//
//
//        }
//
//        else if (degrees.size()<=0) {
//            //学位核实查询
//            SysDegrees sysDegrees = null;
//            DegreesSuveryThree degreesSuveryThree = new DegreesSuveryThree(degreesMapper,degreeDetailsMapper);
//
//            SysReportDetails sysReportDetails13 = new SysReportDetails();
//            BeanUtils.copyProperties(sysReportDetails,sysReportDetails13);
//            sysReportDetails13.setSurveyId(3l);
//            sysReportDetails13.setSurveyName(surveyMap.get(3l));
//            sysReportDetails13.setSurveyStatus(1l);  //设置为调查中
//            sysReportDetails13.setType("12");  //个人信息风险评估
//            sysReportDetails13.setSurveyResult(4l);
//
//            try {
//                sysDegrees = degreesSuveryThree.QueryDegrees(name, degreeCerNo, card, orderId);
//            } catch (Exception e) {
////                    throw new ServiceException("学位核实查询失败");
//                sysReportDetails13.setSurveyResult(4l);
//            }
//
//            if (sysDegrees==null||!sysDegrees.getCode().equals("0000")) {
//                //查询失败
////                    throw new ServiceException("学位核实查询失败");
//                sysReportDetails13.setSurveyResult(4l);
//            }
//            //todo 表插入
//
//
//
//            if (sysDegrees!=null) {
//                if (sysDegrees.getCode().equals("0000")) {
//                    sysReportDetails13.setSurveyResult(2l);
//                }
//            }
//
//
//
//            reportDetailsList.add(sysReportDetails13);
//        }

//        //优先走缓存：
//        List<SysCommercial> commercials = new ArrayList<>();
//        if (historysurveyMap.containsKey(4l)) {
//            //则走对应的历史查询
//            Long historyOrderId = historysurveyMap.get(4l);
//            //todo 复制数据数据并插入新的
//            commercials = commercialMapper.selectSysCommercialByOrderId(historyOrderId);
//
//
//
//
//            if (CollectionUtils.isNotEmpty(commercials)) {
//                SysCommercial historyCommercial = commercials.get(0);
//                SysCommercial sysCommercial = new SysCommercial();
//                BeanUtils.copyProperties(historyCommercial,sysCommercial);
//                sysCommercial.setOrderId(order.getId());
//                sysCommercial.setCreateTime(new Date());
//
//                commercialMapper.insertSysCommercial(sysCommercial);
//
//
//
//                //封装对应的legal_infos
//                List<SysLegalInfos> historylegalInfos = legalInfosMapper.selectSysLegalInfosByCommercialId(historyCommercial.getId());
//
//                if (CollectionUtils.isNotEmpty(historylegalInfos)) {
//                    List<SysLegalInfos> newLegalInfosList = new ArrayList<>();
//
//                    for (SysLegalInfos historylegalInfo : historylegalInfos) {
//                        SysLegalInfos sysLegalInfos = new SysLegalInfos();
//                        BeanUtils.copyProperties(historylegalInfo,sysLegalInfos);
//                        sysLegalInfos.setSysCommercialId(sysCommercial.getId());
//                        newLegalInfosList.add(sysLegalInfos);
//                    }
//
//                    legalInfosMapper.insertBatchSysLegal(newLegalInfosList);
//
//
//
//
//                }
//
//                //封装对应的shareholder_infos
//                List<SysShareholderInfos> historyshareholderInfos = shareholderInfosMapper.selectSysShareholderInfosByCommercialId(historyCommercial.getId());
//                if (CollectionUtils.isNotEmpty(historyshareholderInfos)) {
//                    List<SysShareholderInfos> newShareholderInfosList = new ArrayList<>();
//
//                    for (SysShareholderInfos historyshareholderInfo : historyshareholderInfos) {
//                        SysShareholderInfos sysShareholderInfos = new SysShareholderInfos();
//                        BeanUtils.copyProperties(historyshareholderInfo,sysShareholderInfos);
//                        sysShareholderInfos.setSysCommercialId(sysCommercial.getId());
//                        newShareholderInfosList.add(sysShareholderInfos);
//                    }
//                    shareholderInfosMapper.insertBatchSysShareholderInfos(newShareholderInfosList);
//
//                }
//
//                //封装对应的manager_infos
//                List<SysManagerInfos> historymanagerInfos = managerInfosMapper.selectSysManagerInfosByCommercialId(historyCommercial.getId());
//                if (CollectionUtils.isNotEmpty(historymanagerInfos)) {
//                    List<SysManagerInfos> newSysManagerInfosList = new ArrayList<>();
//
//                    for (SysManagerInfos historymanagerInfo : historymanagerInfos) {
//                        SysManagerInfos sysManagerInfos = new SysManagerInfos();
//                        BeanUtils.copyProperties(historymanagerInfo,sysManagerInfos);
//                        sysManagerInfos.setSysCommercialId(sysCommercial.getId());
//                        newSysManagerInfosList.add(sysManagerInfos);
//                    }
//                    managerInfosMapper.insertBatchSysManager(newSysManagerInfosList);
//                }
//
//                //封装对应的punisheds
//                List<SysPunisheds> historypunisheds = punishedsMapper.selectSysPunishedsByCommercialId(historyCommercial.getId());
//
//                if (CollectionUtils.isNotEmpty(historypunisheds)) {
//                    List<SysPunisheds> newSysPunishedsList = new ArrayList<>();
//
//                    for (SysPunisheds historypunished : historypunisheds) {
//                        SysPunisheds sysPunisheds = new SysPunisheds();
//                        BeanUtils.copyProperties(historypunished,sysPunisheds);
//                        sysPunisheds.setSysCommercialId(sysCommercial.getId());
//                        newSysPunishedsList.add(sysPunisheds);
//                    }
//                    punishedsMapper.insertBatchSysPunisheds(newSysPunishedsList);
//
//                }
//
//                //封装对应的punish_breaks
//                List<SysPunishBreaks> historypunishBreaks = punishBreaksMapper.selectSysPunishBreaksByCommercialId(historyCommercial.getId());
//
//
//                if (CollectionUtils.isNotEmpty(historypunishBreaks)) {
//                    List<SysPunishBreaks> newSysPunishBreaksList = new ArrayList<>();
//
//                    for (SysPunishBreaks historypunishBreak : historypunishBreaks) {
//                        SysPunishBreaks sysPunishBreaks = new SysPunishBreaks();
//                        BeanUtils.copyProperties(historypunishBreak,sysPunishBreaks);
//                        sysPunishBreaks.setSysCommercialId(sysCommercial.getId());
//                        newSysPunishBreaksList.add(sysPunishBreaks);
//                    }
//                    punishBreaksMapper.insertBatchSysPunishBreaks(newSysPunishBreaksList);
//
//
//                }
//
//                //封装对应的admin_penatly
//                List<SysAdminPenalty> historyadminPenalties = adminPenaltyMapper.selectSysAdminPenaltyByCommercialId(historyCommercial.getId());
//
//                if (CollectionUtils.isNotEmpty(historyadminPenalties)) {
//                    List<SysAdminPenalty> newSysAdminPenaltyList = new ArrayList<>();
//
//                    for (SysAdminPenalty historyadminPenalty : historyadminPenalties) {
//                        SysAdminPenalty sysAdminPenalty = new SysAdminPenalty();
//                        BeanUtils.copyProperties(historyadminPenalty,sysAdminPenalty);
//                        sysAdminPenalty.setSysCommercialId(sysCommercial.getId());
//                        newSysAdminPenaltyList.add(sysAdminPenalty);
//                    }
//
//                    adminPenaltyMapper.insertBatchSysAdminPenalty(newSysAdminPenaltyList);
//
//                }
//
//
//
//                //todo 复制风险评估数据并插入
//
//                List<SysReportDetails> reportDetails = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "9");
//
//                if(CollectionUtils.isNotEmpty(reportDetails)) {
//                    List<SysReportDetails> newReportDetails = new ArrayList<>();
//
//                    for (SysReportDetails reportDetail : reportDetails) {
//                        SysReportDetails sysReportDetails1 = new SysReportDetails();
//                        BeanUtils.copyProperties(reportDetail,sysReportDetails1);
//
//                        sysReportDetails1.setOrderId(order.getId());
//                        sysReportDetails1.setUserId(SecurityUtils.getUserId());
//                        sysReportDetails1.setDeptId(SecurityUtils.getDeptId());
//                        sysReportDetails1.setCreateTime(new Date());
//                        sysReportDetails1.setCreateBy(SecurityUtils.getUsername());
//
//                        newReportDetails.add(sysReportDetails1);
//
//
//                    }
//
//                    reportDetailsMapper.insertBatchSysReportDetails(newReportDetails);
//
//
//                }
//
//
//            }
//
//
//
//
//        }
//
//
//        else if(commercials.size()<=0) {
//            //个人工商查询
//            SysCommercial sysCommercial =null;
//            CommercialNameAndCard commercialNameAndCard = new CommercialNameAndCard(commercialMapper,legalInfosMapper,shareholderInfosMapper,managerInfosMapper,punishedsMapper,punishBreaksMapper,adminPenaltyMapper);
//
//            try {
//                sysCommercial = commercialNameAndCard.QueryCommercial(name, card, orderId);
//            } catch (Exception e) {
//                throw new ServiceException("个人工商查询失败");
//            }
//
//            if (sysCommercial==null||(!sysCommercial.getCode().equals("0000")&&!sysCommercial.getCode().equals("1404"))) {
//                //异常
//                throw new ServiceException("个人工商查询失败");
//            }
//            //todo 表插入
//
//            SysReportDetails sysReportDetails10 = new SysReportDetails();
//            BeanUtils.copyProperties(sysReportDetails,sysReportDetails10);
//            sysReportDetails10.setSurveyId(4l);
//            sysReportDetails10.setSurveyName(surveyMap.get(4l));
//            sysReportDetails10.setSurveyStatus(1l);  //设置为调查中
//            sysReportDetails10.setType("9");  //个人工商 商业利益冲突
//            sysReportDetails10.setSurveyResult(2l);
//
//
//            if (sysCommercial.getLegalInfos().size()!=0||sysCommercial.getShareholderInfos().size()!=0||sysCommercial.getManagerInfos().size()!=0) {
//                sysReportDetails10.setSurveyResult(4l);
//
//            }
//
//            reportDetailsList.add(sysReportDetails10);
//
//        }


//        //身份信息验证
//
//        //优先走缓存：
//        List<SysIdentity> identitys = new ArrayList<>();
//        if (historysurveyMap.containsKey(5l)) {
//            //则走对应的历史查询
//            Long historyOrderId = historysurveyMap.get(5l);
//            //todo 复制数据数据并插入新的
//            identitys= identityMapper.selectSysIdentityByOrderId(historyOrderId);
//
//
//            if (CollectionUtils.isNotEmpty(identitys)) {
//                SysIdentity historysysIdentity = identitys.get(0);
//                SysIdentity sysIdentity = new SysIdentity();
//
//                BeanUtils.copyProperties(historysysIdentity,sysIdentity);
//                sysIdentity.setOrderId(order.getId());
//                sysIdentity.setCreateTime(new Date());
//                sysIdentity.setCreateBy(SecurityUtils.getUserId());  //01改
//
//                identityMapper.insertSysIdentity(sysIdentity);
//
//
//
//                //封装对应的 case :dzmx civil implement
//                List<SysIdentityCases> historySysIdentityCases = identityCasesMapper.selectSysIdentityCasesByIdentityIdNoType(historysysIdentity.getId());
//                if (CollectionUtils.isNotEmpty(historySysIdentityCases)) {
//
//
//                    List<SysIdentityDsrxx> dsrxxesList = new ArrayList<>();
//                    for (SysIdentityCases historySysIdentityCase : historySysIdentityCases) {
//                        List<SysIdentityCases> newSysIdentityCasesList = new ArrayList<>();
//                        SysIdentityCases sysIdentityCases = new SysIdentityCases();
//                        BeanUtils.copyProperties(historySysIdentityCase,sysIdentityCases);
//                        sysIdentityCases.setIdentityId(sysIdentity.getId());
//                        newSysIdentityCasesList.add(sysIdentityCases);
//                        identityCasesMapper.insertBatch(newSysIdentityCasesList);
//
//                        //封装dsrxx：
//                        List<SysIdentityDsrxx> identityDsrxxes = dsrxxMapper.selectSysIdentityDsrxxByCasesId(historySysIdentityCase.getId());
//
//                        if (CollectionUtils.isNotEmpty(identityDsrxxes)) {
//                            for (SysIdentityDsrxx identityDsrxx : identityDsrxxes) {
//                                SysIdentityDsrxx sysIdentityDsrxx = new SysIdentityDsrxx();
//                                BeanUtils.copyProperties(identityDsrxx,sysIdentityDsrxx);
//                                sysIdentityDsrxx.setCasesId(sysIdentityCases.getId());
//                                dsrxxesList.add(sysIdentityDsrxx);
//
//                            }
//                        }
//
//                    }
//
//                    dsrxxMapper.insertBatch(dsrxxesList);
//
//
//
//
//
//
//
//
//
//
//                }
//                //封装对应的 sxbzxr
//                List<SysIdentitySxbzxr> historyidentitySxbzxrs = identitySxbzxrMapper.selectSysIdentitySxbzxrByIdentityId(historysysIdentity.getId());
//
//
//                if (CollectionUtils.isNotEmpty(historyidentitySxbzxrs)) {
//
//                    List<SysIdentitySxbzxr> newSysIdentitySxbzxrList = new ArrayList<>();
//
//                    for (SysIdentitySxbzxr historyidentitySxbzxr : historyidentitySxbzxrs) {
//                        SysIdentitySxbzxr sysIdentitySxbzxr = new SysIdentitySxbzxr();
//                        BeanUtils.copyProperties(historyidentitySxbzxr,sysIdentitySxbzxr);
//                        sysIdentitySxbzxr.setIdentityId(sysIdentity.getId());
//                        newSysIdentitySxbzxrList.add(sysIdentitySxbzxr);
//                    }
//
//
//                    identitySxbzxrMapper.insertBatch(newSysIdentitySxbzxrList);
//
//                }
//
//                //封装对应的 xgbzxr
//                List<SysIdentityXgbzxr> historyxgbzxrs = xgbzxrMapper.selectSysIdentityXgbzxrByIdentityId(historysysIdentity.getId());
//
//
//
//                if (CollectionUtils.isNotEmpty(historyxgbzxrs)) {
//
//                    List<SysIdentityXgbzxr> newSysIdentityXgbzxrList = new ArrayList<>();
//                    for (SysIdentityXgbzxr historyxgbzxr : historyxgbzxrs) {
//
//                        SysIdentityXgbzxr sysIdentityXgbzxr = new SysIdentityXgbzxr();
//                        BeanUtils.copyProperties(historyxgbzxr,sysIdentityXgbzxr);
//                        sysIdentityXgbzxr.setIdentityId(sysIdentity.getId());
//                        newSysIdentityXgbzxrList.add(sysIdentityXgbzxr);
//                    }
//
//                    xgbzxrMapper.insertBatch(newSysIdentityXgbzxrList);
//
//                }
//
//                //todo 复制风险评估数据并插入
//
//                List<SysReportDetails> reportDetails01 = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "0");
//                List<SysReportDetails> reportDetails02 = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "3");
//                List<SysReportDetails> reportDetails03 = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "4");
//                List<SysReportDetails> reportDetails04 = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "5");
//                List<SysReportDetails> reportDetails05 = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "10");
//
//                List<SysReportDetails> reportDetails = Stream.of(reportDetails01,reportDetails02,reportDetails03,reportDetails04,reportDetails05).flatMap(List::stream).collect(Collectors.toList());
//
//                if(CollectionUtils.isNotEmpty(reportDetails)) {
//                    List<SysReportDetails> newReportDetails = new ArrayList<>();
//
//                    for (SysReportDetails reportDetail : reportDetails) {
//                        SysReportDetails sysReportDetails1 = new SysReportDetails();
//                        BeanUtils.copyProperties(reportDetail,sysReportDetails1);
//
//                        sysReportDetails1.setOrderId(order.getId());
//                        sysReportDetails1.setUserId(SecurityUtils.getUserId());
//                        sysReportDetails1.setDeptId(SecurityUtils.getDeptId());
//                        sysReportDetails1.setCreateTime(new Date());
//                        sysReportDetails1.setCreateBy(SecurityUtils.getUsername());
//
//                        newReportDetails.add(sysReportDetails1);
//
//
//                    }
//
//                    reportDetailsMapper.insertBatchSysReportDetails(newReportDetails);
//
//
//                }
//
//
//            }
//
//
//
//
//        }
//
//        else if (identitys.size()<=0) {
//            //身份信息查询
//            IdentitySuveryImp identitySuveryImp = new IdentitySuveryImp(identityMapper,identityCasesMapper,dsrxxMapper,identitySxbzxrMapper,xgbzxrMapper);
//
//            List<SysIdentity> sysIdentities = identitySuveryImp.QueryIndentity(name, card, 1);
//
//
//            if (sysIdentities==null||sysIdentities.size()<1) {
//                throw new ServiceException("个人信息验证查询失败");
//            }
//            for (SysIdentity sysIdentity : sysIdentities) {
//                if (!sysIdentity.getCode().equals("1000")) {
//                    throw new ServiceException("个人信息验证查询失败");
//                }
//            }
//
//            //todo
//            for (SysIdentity sysIdentity : sysIdentities) {
//
//                String identity = sysIdentity.getIdentity();
//                SysReportDetails sysReportDetails1 = new SysReportDetails();
//                BeanUtils.copyProperties(sysReportDetails,sysReportDetails1);
//                sysReportDetails1.setSurveyId(5l);
//                sysReportDetails1.setSurveyName(surveyMap.get(5l));
//                sysReportDetails1.setSurveyStatus(1l);  //设置为调查中
//                sysReportDetails1.setType("0");  //个人信息风险评估
//
//                if (identity.equals("验证一致")) {
//
//                    sysReportDetails1.setSurveyResult(2l);  //无风险
//
//                }
//                else  {
//                    sysReportDetails1.setSurveyResult(4l);   //中风险
//                }
//
//                //被执行人风险评估
//                SysReportDetails sysReportDetails4 = new SysReportDetails();
//                BeanUtils.copyProperties(sysReportDetails1,sysReportDetails4);
//                sysReportDetails4.setType("3");
//                sysReportDetails4.setSurveyStatus(2l);
//                Long identityId = sysIdentity.getId();
//                List<SysIdentityCases> identityCases= identityCasesMapper.selectImplementNssdwByIdentityId(identityId);
//                for (SysIdentityCases identityCase : identityCases) {
//                    String nSsdw = identityCase.getNSsdw();
//
//                    if (nSsdw!=""||!nSsdw.equals("")) {
//                        //说明有数据
//                        sysReportDetails4.setSurveyStatus(4l);
//                    }
//                }
//                //失信被执行人评估：
//                SysReportDetails sysReportDetails5 = new SysReportDetails();
//                BeanUtils.copyProperties(sysReportDetails1,sysReportDetails5);
//                sysReportDetails5.setType("4");
//                sysReportDetails5.setSurveyStatus(2l);
//
//
//
//                List<SysIdentitySxbzxr> identitySxbzxrs=identitySxbzxrMapper.selectSysIdentitySxbzxrByIdentityId(identityId);
//                if (!CollectionUtils.isEmpty(identitySxbzxrs)) {
//                    sysReportDetails5.setSurveyResult(4l);
//                }
//
//                //限高被执行人评估：
//                SysReportDetails sysReportDetails6 = new SysReportDetails();
//                BeanUtils.copyProperties(sysReportDetails1,sysReportDetails6);
//                sysReportDetails6.setType("5");
//                sysReportDetails6.setSurveyStatus(2l);
//                List<SysIdentityXgbzxr> xgbzxrs=xgbzxrMapper.selectByIdentityId(identityId);
//
//                if (CollectionUtils.isNotEmpty(xgbzxrs)) {
//                    sysReportDetails6.setSurveyStatus(4l);
//                }
//
//
//                //法院诉讼风险评估
//                SysReportDetails sysReportDetails11 = new SysReportDetails();
//                BeanUtils.copyProperties(sysReportDetails1,sysReportDetails11);
//                sysReportDetails11.setType("10");
//                sysReportDetails11.setSurveyStatus(2l);
//                List<SysIdentityCases> civils=identityCasesMapper.selectSysIdentityCasesByIdentityId(identityId,"1");
//                List<SysIdentityCases> implementes=identityCasesMapper.selectSysIdentityCasesByIdentityId(identityId,"2");
//                if (CollectionUtils.isNotEmpty(civils)||CollectionUtils.isNotEmpty(implementes)) {
//                    sysReportDetails11.setSurveyStatus(4l);
//                }
//
//                //todo 到这了
//
//
//
//
//                reportDetailsList.add(sysReportDetails1);
//                reportDetailsList.add(sysReportDetails4);
//                reportDetailsList.add(sysReportDetails5);
//                reportDetailsList.add(sysReportDetails6);
//                reportDetailsList.add(sysReportDetails11);
//
//            }
//        }


//
//        //优先走缓存：
//        List<SysNegative> negatives = new ArrayList<>();
//        if (historysurveyMap.containsKey(6l)) {
//            //则走对应的历史查询
//            Long historyOrderId = historysurveyMap.get(6l);
//            //todo 复制数据数据并插入新的
//            negatives = negativeMapper.selectSysNegativeByOrderId(historyOrderId);
//            if (CollectionUtils.isNotEmpty(negatives)) {
//                SysNegative historysysNegative = negatives.get(0);
//                SysNegative sysNegative = new SysNegative();
//                BeanUtils.copyProperties(historysysNegative, sysNegative);
//                sysNegative.setOrderId(order.getId());
//                sysNegative.setCreateTime(new Date());
//                sysNegative.setCreateBy(SecurityUtils.getUserId());
//
//                negativeMapper.insertSysNegative(sysNegative);
//
//                //封装对应的学位详细表：
//                List<SysNegativeDetails> historyNegativeDetails = negativeDetailsMapper.selectSysNegativeDetailsByNegativeId(historysysNegative.getId());
//
//                if (CollectionUtils.isNotEmpty(historyNegativeDetails)) {
//                    List<SysNegativeDetails> newSysNegativeDetailsList = new ArrayList<>();
//
//                    for (SysNegativeDetails historyNegativeDetail : historyNegativeDetails) {
//                        SysNegativeDetails sysNegativeDetails = new SysNegativeDetails();
//                        BeanUtils.copyProperties(historyNegativeDetail, sysNegativeDetails);
//                        sysNegativeDetails.setSysNegativeId(sysNegative.getId());
//                        newSysNegativeDetailsList.add(sysNegativeDetails);
//
//                    }
//                    negativeDetailsMapper.insertBatch(newSysNegativeDetailsList);
//
//
//                }
//
//                //todo 复制风险评估数据并插入
//
//                List<SysReportDetails> reportDetails01 = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "1");
//                List<SysReportDetails> reportDetails02 = reportDetailsMapper.selectSysReportByOrderIdAndType(historyOrderId, "2");
//
//                List<SysReportDetails> reportDetails = Stream.concat(reportDetails01.stream(), reportDetails02.stream()).collect(Collectors.toList());
//                if (CollectionUtils.isNotEmpty(reportDetails)) {
//                    List<SysReportDetails> newReportDetails = new ArrayList<>();
//
//                    for (SysReportDetails reportDetail : reportDetails) {
//                        SysReportDetails sysReportDetails1 = new SysReportDetails();
//                        BeanUtils.copyProperties(reportDetail, sysReportDetails1);
//
//                        sysReportDetails1.setOrderId(order.getId());
//                        sysReportDetails1.setUserId(SecurityUtils.getUserId());
//                        sysReportDetails1.setDeptId(SecurityUtils.getDeptId());
//                        sysReportDetails1.setCreateTime(new Date());
//                        sysReportDetails1.setCreateBy(SecurityUtils.getUsername());
//
//                        newReportDetails.add(sysReportDetails1);
//
//
//                    }
//
//                    reportDetailsMapper.insertBatchSysReportDetails(newReportDetails);
//
//
//                }
//
//
//            } else if (negatives.size() <= 0) {
//                //公安不良查询
//                NegativeSuvery negativeSuvery = new NegativeSuvery(negativeMapper, negativeDetailsMapper);
//                SysNegative sysNegative = negativeSuvery.QueryNegative(name, card, 1, orderId);
//                if (sysNegative == null || !sysNegative.getCode().equals("1000")) {
//                    throw new ServiceException("公安不良查询失败");
//                }
//
//                //todo 表插入
//
//                SysReportDetails sysReportDetails2 = new SysReportDetails();
//                BeanUtils.copyProperties(sysReportDetails, sysReportDetails2);
//                sysReportDetails2.setSurveyId(6l);
//                sysReportDetails2.setSurveyName(surveyMap.get(6l));
//                sysReportDetails2.setSurveyStatus(1l);  //设置为调查中
//                sysReportDetails2.setType("1");  //个人信息风险评估
//
//                //吸毒信息评估
//                SysReportDetails sysReportDetails3 = new SysReportDetails();
//                BeanUtils.copyProperties(sysReportDetails2, sysReportDetails3);
//                sysReportDetails3.setType("2");
//
//                List<SysNegativeDetails> negativeDetails = sysNegative.getNegativeDetails();
//                for (SysNegativeDetails negativeDetail : negativeDetails) {
//                    if (negativeDetail.getHit().equals("0")) {
//                        //说明无风险
//                        sysReportDetails2.setSurveyResult(2l);
//                        sysReportDetails3.setSurveyResult(2l);
//                    } else if (negativeDetail.getHit().equals("1")) {
//                        //说明有风险
//                        Integer score = negativeDetail.getScore();
//                        String scoreBin = toSixDigitBinaryString(score);
//                        boolean b = checkBits(scoreBin);
//                        boolean a = isFourthBitZero(scoreBin);
//                        sysReportDetails2.setSurveyResult(2l);
//                        sysReportDetails3.setSurveyStatus(2l);
//                        if (!b) {
//                            sysReportDetails2.setSurveyResult(4l);
//                        }
//                        if (!a) {
//                            sysReportDetails3.setSurveyStatus(4l);
//                        }
//                    }
//                    reportDetailsList.add(sysReportDetails2);
//                    reportDetailsList.add(sysReportDetails3);
//
//                }
//
//
//            }
//        }
//
//

//

    }










    private SysNegative  getNegative(String code) {
        SysNegative sysNegative = JSONObject.parseObject(code, SysNegative.class);

        List<JSONObject> datas = JSONObject.parseObject(String.valueOf(sysNegative.getData()), List.class);

        sysNegative.setCreateTime(new Date());
        sysNegative.setCreateBy(SecurityUtils.getUserId());



        return sysNegative;
    }





    private List<SysIdentity>  getIdentityEntity(String code) {
        SysIdentity sysIdentity = JSONObject.parseObject(code, SysIdentity.class);

        List<SysIdentity> sysIdentities = new ArrayList<>();
                //说明是多个数据
                List<JSONObject> jsonObjects = JSONObject.parseObject(sysIdentity.getData(),List.class);
                for (JSONObject jsonObject : jsonObjects) {
                    SysIdentity sysIdentity01 = new SysIdentity();
                    BeanUtils.copyProperties(sysIdentity,sysIdentity01);
                    sysIdentity01.setCardId(String.valueOf(jsonObject.get("id")));
                    sysIdentity01.setIdentity((String) jsonObject.get("identity"));

                    if (jsonObject.get("dzmx")!=null) {
                        List<JSONObject> dzmx = JSONObject.parseObject(jsonObject.get("dzmx").toString(),List.class);

                        sysIdentity01.setDzmx(dzmx);
                    }
                    if (jsonObject.get("gkmx")!=null) {
                        JSONObject gkmx = JSONObject.parseObject(jsonObject.get("gkmx").toString());
                        sysIdentity01.setCrc(String.valueOf(gkmx.get("crc")));
                        if (gkmx.get("civil")!=null) {
                            JSONObject civil = JSONObject.parseObject(String.valueOf(gkmx.get("civil")));
                            List<JSONObject> civilCases = JSONObject.parseObject(String.valueOf(civil.get("cases")), List.class);
                            sysIdentity01.setCivil(civilCases);
//                            for (JSONObject civilCase : civilCases) {
//                                if (civilCase.get("c_dsrxx")!=null) {
//                                    List<JSONObject> c_dsrxx = JSONObject.parseObject(civilCase.get("c_dsrxx").toString(), List.class);
//
//                                }
//                            }

                        }
                        if (gkmx.get("implement")!=null) {
                            JSONObject implement = JSONObject.parseObject(String.valueOf(gkmx.get("implement")));
                            List<JSONObject> implementCases = JSONObject.parseObject(String.valueOf(implement.get("cases")), List.class);

                            sysIdentity01.setImplement(implementCases);
                        }




                    }

                    if (jsonObject.get("sxbzxr")!=null) {
                        List<JSONObject> sxbzxr = JSONObject.parseObject(jsonObject.get("sxbzxr").toString(),List.class);

                        sysIdentity01.setSxbzxr(sxbzxr);
                    }
                    if (jsonObject.get("xgbzxr")!=null) {
                        List<JSONObject> xgbzxr = JSONObject.parseObject(jsonObject.get("xgbzxr").toString(), List.class);

                        sysIdentity01.setXgbzxr(xgbzxr);
                    }
                    sysIdentity01.setCreateTime(new Date());

                    //todo
                    sysIdentity01.setCreateBy(1L);
                    sysIdentities.add(sysIdentity01);

                }
                return sysIdentities;

        }





    private SysCommercial getCommercialEntity(String code) {
        SysCredit sysCredit = JSONObject.parseObject(code, SysCredit.class);

        SysMessage sysMessage = JSONObject.parseObject(sysCredit.getMessage(), SysMessage.class);
        String data = sysMessage.getData();
        SysCommercial sysCommercial = new SysCommercial();
        if (!StringUtils.isEmpty(data)){
            JSONObject SysCommercials = JSONObject.parseObject(sysMessage.getData());
            List<JSONObject> legalInfos = (List<JSONObject>) SysCommercials.get("legalInfos");
            List<JSONObject> shareholderInfos = ( List<JSONObject>)SysCommercials.get("shareholderInfos");
            List<JSONObject> managerInfos =(List<JSONObject>) SysCommercials.get("managerInfos");
            List<JSONObject> punisheds = ( List<JSONObject>)SysCommercials.get("punisheds");
            List<JSONObject> punishBreaks =(List<JSONObject> ) SysCommercials.get("punishBreaks");
            List<JSONObject> adminPenalty =(List<JSONObject>) SysCommercials.get("adminPenalty");

            sysCommercial.setLegalInfos(legalInfos);
            sysCommercial.setShareholderInfos(shareholderInfos);
            sysCommercial.setManagerInfos(managerInfos);
            sysCommercial.setPunisheds(punisheds);
            sysCommercial.setPunishBreaks(punishBreaks);
            sysCommercial.setAdminPenalties(adminPenalty);
            //处理legalinfos

        }




//        SysDegrees sysDegrees = new SysDegrees();

        BeanUtils.copyProperties(sysCredit,sysCommercial);
        BeanUtils.copyProperties(sysMessage,sysCommercial);

        sysCommercial.setOutTradeNo(sysCredit.getOut_trade_no());
        sysCommercial.setTranAmt(sysCredit.getTran_amt());
        sysCommercial.setTranTime(sysCredit.getTran_time());
        sysCommercial.setCreateTime(new Date());
//        return sysEducation;
        return sysCommercial;
    }

    private SysEducation getEducationEntity(String code) {
        SysCredit sysCredit = JSONObject.parseObject(code, SysCredit.class);

        SysMessage sysMessage = JSONObject.parseObject(sysCredit.getMessage(), SysMessage.class);
        String data = sysMessage.getData();
        SysEducation sysEducation = new SysEducation();
        if (!StringUtils.isEmpty(data)){
            List<JSONObject> jsonObjects =new ArrayList<>();
            if (data.charAt(0)=='{') {
                //说明只有一个数据
                JSONObject sysDegreeDetails = JSONObject.parseObject(sysMessage.getData());
                jsonObjects.add(sysDegreeDetails);
            }
            else {
                //有多个数据
                List<JSONObject> sysDegreeDetails = JSONObject.parseObject(sysMessage.getData(), List.class);

                jsonObjects=sysDegreeDetails;
            }


            sysEducation.setJsonObjects(jsonObjects);

        }




//        SysDegrees sysDegrees = new SysDegrees();

        BeanUtils.copyProperties(sysCredit,sysEducation);
        BeanUtils.copyProperties(sysMessage,sysEducation);

        sysEducation.setOutTradeNo(sysCredit.getOut_trade_no());
        sysEducation.setTranAmt(sysCredit.getTran_amt());
        sysEducation.setTranTime(sysCredit.getTran_time());
        sysEducation.setCreateTime(new Date());
        return sysEducation;
    }

    private SysDegrees getDegreesEntity(String code) {
        SysCredit sysCredit = JSONObject.parseObject(code, SysCredit.class);

        SysMessage sysMessage = JSONObject.parseObject("{\"result\":\"0\",\"resultMsg\":\"唯一匹配/多个匹配\",\"data\":[{\"xm\":\"张三\",\"xb\":\"男\",\"csrq\":\"1988-05-19\",\"xwsydw\":\"北京大学\",\"xkzy\":\"软件工程\",\"xxgdlx\":\"博士\",\"xwlb\":\"工学博士学位\",\"xwzsbh\":\"8868***686\",\"hxwrq\":\"2008-08-08\"},{\"xm\":\"张三\",\"xb\":\"男\",\"csrq\":\"1988-05-19\",\"xwsydw\":\"北京大学\",\"xkzy\":\"软件工程\",\"xxgdlx\":\"博士\",\"xwlb\":\"工学博士学位\",\"xwzsbh\":\"8868***686\",\"hxwrq\":\"2008-08-08\"}]}", SysMessage.class);
        SysDegrees sysDegrees = new SysDegrees();
        String data = sysMessage.getData();
        if (!StringUtils.isEmpty(data)){
            List<JSONObject> jsonObjects =new ArrayList<>();
            if (data.charAt(0)=='{') {
                //说明只有一个数据
                JSONObject sysDegreeDetails = JSONObject.parseObject(sysMessage.getData());
                jsonObjects.add(sysDegreeDetails);
            }
            else {
                //有多个数据
                List<JSONObject> sysDegreeDetails = JSONObject.parseObject(sysMessage.getData(), List.class);

                jsonObjects=sysDegreeDetails;
            }



            sysDegrees.setJsonObjects(jsonObjects);
        }






        BeanUtils.copyProperties(sysCredit,sysDegrees);
        BeanUtils.copyProperties(sysMessage,sysDegrees);

        sysDegrees.setOutTradeNo(sysCredit.getOut_trade_no());
        sysDegrees.setTranAmt(sysCredit.getTran_amt());
        sysDegrees.setTranTime(sysCredit.getTran_time());
        sysDegrees.setCreateTime(new Date());
        return sysDegrees;
    }

    private SysCreditEntity getSysCredit(String code) {
        SysCredit sysCredit = JSONObject.parseObject(code, SysCredit.class);

        SysMessage sysMessage = JSONObject.parseObject(sysCredit.getMessage(), SysMessage.class);

        SysData sysData = JSONObject.parseObject(sysMessage.getData(), SysData.class);

        SysCreditEntity sysCreditEntity = new SysCreditEntity();
        sysCreditEntity.setCode(sysCredit.getCode());
        sysCreditEntity.setOutTradeNo(sysCredit.getOut_trade_no());
        sysCreditEntity.setTranAmt(sysCredit.getTran_amt());
        sysCreditEntity.setTranTime(sysCredit.getTran_time());
        sysCreditEntity.setSign(sysCredit.getSign());
        sysCreditEntity.setResult(sysMessage.getResult());
        sysCreditEntity.setResultMsg(sysMessage.getResultMsg());
        sysCreditEntity.setDataTag(sysData.getTag());
        sysCreditEntity.setDataResult(sysData.getResult());
        sysCreditEntity.setDataMessage(sysData.getMessage());
        sysCreditEntity.setDataBadPlatformNum(sysData.getBadPlatformNum());
        sysCreditEntity.setDataGoodPlatformNum(sysData.getGoodPlatformNum());
        sysCreditEntity.setDataMaxMoney(sysData.getMaxMoney());
        sysCreditEntity.setDataLastTime(sysData.getLastTime());
        sysCreditEntity.setDataMaxDay(sysData.getMaxDay());


        return sysCreditEntity;
    }
}
